// 服务器框架express
import express from 'express';
// import { createProxyMiddleware } from 'http-proxy-middleware';
import cors from 'cors';
// 导入解密token的模块
import jwt from 'express-jwt';
// 导入商品路由
import goodsRouter from './router/goodsRouter/index.js';
// 导入用户路由
import userRouter from './router/userRouter/userRouter.js';
// const options = {
//   target: 'http://39.98.123.211', // target host
//   changeOrigin: true, // needed for virtual hosted sites
//   ws: true, // proxy websockets

// }
// const exampleProxy = createProxyMiddleware(options);
const app=express();
// app.use(['/api/product', '/api/list', '/api/item/'], exampleProxy);
app.use(cors())
app.use(express.static('dist'))
app.use(express.urlencoded({extended:false}))
app.use(express.json())


app.use(jwt({ secret: 'xiaoliu123^_^', algorithms: ['HS256'] }).unless({ path: [/goods|product/] }));
// 解析post提交内容
app.use('/api',goodsRouter)
app.use('/api',userRouter)

// 6. 在路由之后去定义错误级别中间件
app.use(function(err, req, res, next) {
  if (err.name === 'UnauthorizedError') {
      return res.send({
          status: 1,
          msg: '身份认证失败'
      })
  }
})



app.listen(8887,function () {
  console.log("服务器启动成功！");
})